package com.example.lib_net.common

import androidx.lifecycle.LifecycleOwner

/**
 * @author ytx
 * @createDate 2022/8/8 14:51
 * @description
 *
 * @updateUser ytx
 * @updateDate 2022/8/8 14:51
 * @updateRemark
 *
 * @version 1.0.0
 */
class StateObserver private constructor(var mOwner: LifecycleOwner) : ApiStateObserver() {

    init {
        register()
    }

    private var stateChangedListener: StateChangedListener?=null

    fun setStateChangedListener(_stateChangedListener: StateChangedListener){

        stateChangedListener=_stateChangedListener
    }

    override fun apiStateChanged(it: Int) {
        stateChangedListener?.stateChanged(it)
    }

    override fun setLifeCycleOwner(): LifecycleOwner {
        return mOwner
    }

    /**
     * 构建者
     */

    companion object Builder{
        private var owner: LifecycleOwner?  = null
        fun setOwner(_owner: LifecycleOwner):Builder{
            owner = _owner
            return this
        }

        fun build(): StateObserver{
            return StateObserver(owner!!)
        }
    }

    interface StateChangedListener {
        fun stateChanged(it: Int)
    }
}